﻿using System;
using System.Windows;
using System.Threading.Tasks;

namespace FeiYang
{
    /// <summary>
    /// App.xaml 的交互逻辑
    /// </summary>
    public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            挂载异常处理();
            base.OnStartup(e);
        }

        private void 挂载异常处理()
        {
            TaskScheduler.UnobservedTaskException += 未处置的线程异常;
            AppDomain.CurrentDomain.UnhandledException += 未处理的非UI线程异常;
            this.DispatcherUnhandledException += new System.Windows.Threading.DispatcherUnhandledExceptionEventHandler(未处理的UI线程异常);
        }

        private static void 未处理的非UI线程异常(object obj, UnhandledExceptionEventArgs arg)
        {
            var e = arg.ExceptionObject as Exception;
            if (e != null)
            {
                MessageBox.Show(e.Message);
            }
        }
        private static void 未处置的线程异常(object obj, UnobservedTaskExceptionEventArgs arg)
        {
            var e = arg.Exception;
            if (e != null)
            {
                MessageBox.Show(e.Message);
            }
        }
        void 未处理的UI线程异常(object ob, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs arg)
        {
            var ex = arg.Exception;
            MessageBox.Show(ex.Message);
        }
    }
}
